﻿using QD.UpDownLoad;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using QD.SQLHelper;
using System.Configuration;

namespace QD.SyncServers
{
    public partial class SyncStep3 : BaseFrm
    {
        public SyncStep3()
        {
            InitializeComponent();

            string connStr_plat = DbConfig.Instance.GetDbDataBaseConnStr();
            DbHelperSQL db_plat = new DbHelperSQL(connStr_plat);
            DataTable dtDBList = db_plat.Query(string.Format("SELECT * FROM QDDataBase")).Tables[0];
            this.cbxSourceProductId.DataSource = dtDBList;
            this.cbxSourceProductId.DisplayMember = "QfName";
            this.cbxSourceProductId.ValueMember = "QfCode";

            //取目标产品(从目标服务器上读取数据库QDDevPlat_V4中的QDDataBase表数据)
            string connStr = "Data Source=" + ConfigInstanceHelper.ConfigInstance.TargetServerName + ";User ID=" + ConfigInstanceHelper.ConfigInstance.TargetUserId + ";Password=" + ConfigInstanceHelper.ConfigInstance.TargetUserPassword + ";Initial Catalog=QDDevPlat_V4; persist security info=true";
            SqlConnection conn = new SqlConnection(connStr);
            string selectCommand = "SELECT QfCode,(QfName+'('+QfCode+')') as QfName,SerialNumber,MasterDataBase,ConfigDataBase FROM QDDataBase";
            try
            {
                conn.Open();

                //获取目标服务器上所有产品
                DataSet dataSet = new DataSet();
                SqlDataAdapter sqlDA = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand(selectCommand, conn);
                cmd.CommandTimeout = 0;
                cmd.CommandType = CommandType.Text;
                sqlDA.SelectCommand = cmd;
                sqlDA.Fill(dataSet);

                //填充目标产品控件
                this.cbxTargetProductId.DataSource = dataSet.Tables[0];
                this.cbxTargetProductId.DisplayMember = "QfName";
                this.cbxTargetProductId.ValueMember = "QfCode";

                conn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //默认选择目标产品第一项            
            if (this.cbxTargetProductId.Items.Count > 0)
            {
                this.cbxTargetProductId.SelectedIndex = 0;
            }
        }

        private void SyncStep3_Load(object sender, EventArgs e)
        {
            this.label3.Text = "请选择目标及来源产品";
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form frm = Application.OpenForms["SyncStep2"];
            if (frm == null)
            {
                frm = new SyncStep2();
                frm.ShowDialog();
            }
            else
            {
                frm.Show();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            string sourceSerialNumber = "";
            string targetSerialNumber = "";
            //取来源产品主数据库和配置数据库
            object itemSource = this.cbxSourceProductId.SelectedItem;
            if (itemSource != null)
            {
                System.Data.DataRowView drv = (System.Data.DataRowView)itemSource;
                ConfigInstanceHelper.ConfigInstance.SourceMasterDB = string.Concat(drv.Row["MasterDataBase"]);
                ConfigInstanceHelper.ConfigInstance.SourceConfigDB = string.Concat(drv.Row["ConfigDataBase"]);
                ConfigInstanceHelper.ConfigInstance.SourceProductName = string.Concat(drv.Row["QfName"]);
                sourceSerialNumber = string.Concat(drv.Row["SerialNumber"]);
            }

            //取目标产品主数据库和配置数据库
            object itemTarget = this.cbxTargetProductId.SelectedItem;
            if (itemTarget != null)
            {
                System.Data.DataRowView drv = (System.Data.DataRowView)itemTarget;
                ConfigInstanceHelper.ConfigInstance.TargetMasterDB = string.Concat(drv.Row["MasterDataBase"]);
                ConfigInstanceHelper.ConfigInstance.TargetConfigDB = string.Concat(drv.Row["ConfigDataBase"]);
                ConfigInstanceHelper.ConfigInstance.TargetProductName = string.Concat(drv.Row["QfName"]);
                targetSerialNumber = string.Concat(drv.Row["SerialNumber"]);
            }

            //启用事务一致性
            ConfigInstanceHelper.ConfigInstance.EnabledTran = this.cbxEnabledTran.Checked;

            if (string.IsNullOrEmpty(sourceSerialNumber) || string.IsNullOrEmpty(targetSerialNumber))
            {
                MessageBox.Show("产品序列号不能为空！");
                return;
            }
            else if (sourceSerialNumber != targetSerialNumber)
            {
                MessageBox.Show("来源产品序列号与目标产品序列号必须相等！");
                return;
            }

            this.Hide();
            Form frm = Application.OpenForms["SyncStep4"];
            if (frm == null)
            {
                frm = new SyncStep4();
                frm.ShowDialog();
            }
            else
            {
                frm.Show();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void cbxTargetProductId_SelectedIndexChanged(object sender, EventArgs e)
        {
            //根据序列号过滤来源产品
            object item = this.cbxTargetProductId.SelectedItem;
            if (item != null)
            {
                System.Data.DataRowView drv = (System.Data.DataRowView)item;
                string serialNumber = string.Concat(drv.Row["SerialNumber"]);

                this.txtTargetSerialNumber.Text = serialNumber;
                if (this.cbxSourceProductId.DataSource != null)
                {
                    DataTable dtSource = (DataTable)this.cbxSourceProductId.DataSource;
                    dtSource.DefaultView.RowFilter = "SerialNumber = '" + serialNumber + "'";
                }
            }
        }

        private void cbxEnabledTran_CheckedChanged(object sender, EventArgs e)
        {
            if (!this.cbxEnabledTran.Checked)
            {
                MessageBox.Show("如果启用事务一致性，有任何错误会全部回滚，如果不启用事务一致性，发生错误后会继续执行，如错误发生在表数据转移过程中，可能会导致操作表数据丢失，建议启用事务一致性。");
            }
        }
    }
}
